var util = require('../../utils/util')
var QQMapWX  = require('../../utils/qqmap-wx-jssdk.js'); //引入SDK核心类
var qqmapsdk;

const DB = wx.cloud.database()

Page({
  data: {
    flag:true,
    driver:"",
    bus:"",
    date:"",
    time1:"",
    time2:"",
    isShow:false,
    station:"",
    start_station:"",
    last_station:""
  },

  onLoad:function(){
    var that = this
    that.setData({
      driver:"树读 - 18291673213",
      bus:"915路 - 陕A00000",
      // flag:false,    //接收二维码参数
    })
    wx.setStorageSync("driverKey",that.data.driver);
    wx.setStorageSync("busKey",that.data.bus);

    //实例化API核心类
    qqmapsdk = new QQMapWX({
      key:'AHBBZ-V5I3F-GLNJ2-JVBQO-OIVRV-RZFJZ'
    })

    //获取当前地理位置
    qqmapsdk.reverseGeocoder({
      success:function(res){
        var locat = res.result.location.lat.toString() + "," + res.result.location.lng.toString()
        qqmapsdk.search({
          location:locat,
          keyword:'公交车站',
          page_size:'1',
          filter:'category=%E5%9F%BA%E7%A1%80%E8%AE%BE%E6%96%BD,%E4%BA%A4%E9%80%9A%E8%AE%BE%E6%96%BD,%E5%85%AC%E4%BA%A4%E8%BD%A6%E7%AB%99',
          success:function(res){
            //console.log(res)
            var date = util.formatTime1(new Date())
            wx.setStorageSync("dateKey",date); //缓存日期到本地
            that.setData({
              date:wx.getStorageSync("dateKey")
            })

            var str = res.data[0].title
            // 截取最后一个前面的字符
            var index = str.lastIndexOf("[")
            str = str.substring(0,index);

            /* --------------------------------------↓-↓- 测试 -↓-↓-------------------------------------- */
            var start_Time = wx.getStorageSync('time1Key')
            if(start_Time == ''){
              wx.setStorageSync("station1Key",str); //缓存上车地点到本地
              that.setData({
                start_station:wx.getStorageSync('station1Key'),
              })

              var start_time = util.formatTime2(new Date())
              wx.setStorageSync("time1Key",start_time); //缓存上车时间到本地
              that.setData({
                time1:wx.getStorageSync('time1Key'),
                isShow:true
              })
            }else{
              wx.setStorageSync("station2Key",str); //缓存下车地点到本地
              that.setData({
                last_station:wx.getStorageSync('station2Key'),
              })

              var last_time = util.formatTime2(new Date())
              wx.setStorageSync("time2Key",last_time);
              that.setData({
                time2:last_time,
                flag:false,
                isShow:true
              })
              DB.collection("user").add({
                data:{
                  date:that.data.date,  //日期
                  start_Time:wx.getStorageSync('time1Key'),  //上车时间
                  start_station:wx.getStorageSync('station1Key'),  //上车地点
                  last_Time:that.data.time2,  //下车时间
                  last_station:wx.getStorageSync('station2Key'),  //下车地点
                  bus:that.data.bus
                }
              })

              DB.collection("users").add({
                data:{
                  openid:wx.getStorageSync("openidKey"),
                  name:wx.getStorageSync("nameKey"),
                  phone:wx.getStorageSync("phoneKey"),
                  start_time:wx.getStorageSync("dateKey").toString() + " " + wx.getStorageSync("time1Key").toString(),
                  last_time:wx.getStorageSync("dateKey").toString() + " " + wx.getStorageSync("time2Key").toString(),
                  station:wx.getStorageSync("station1Key").toString() + " — " + wx.getStorageSync("station2Key").toString(),
                  bus_info:wx.getStorageSync("busKey"),
                  driver:wx.getStorageSync("driverKey"),
                }
              })
              wx.removeStorageSync("dateKey")  //删除缓存
              wx.removeStorageSync("time1Key")
              wx.removeStorageSync("station1Key")
              wx.removeStorageSync("station2Key")
              wx.removeStorageSync("busKey")
              wx.removeStorageSync("driverKey")
            }
            /* --------------------------------------↑-↑- 测试 -↑-↑-------------------------------------- */


            /* -----------------------------------↓-↓- 接收二维码参数 -↓-↓------------------------------------ */
            // if (that.data.flag) {
            //     var start_Time = wx.getStorageSync('time1Key')
            //     that.setData({
            //     date:wx.getStorageSync("dateKey")
            //     })
            
            //     if (start_Time == '') {
            //         wx.setStorageSync("station1Key", str); //缓存上车地点到本地
            //         that.setData({
            //             start_station: wx.getStorageSync('station1Key'),
            //         })
            
            //         var start_time = util.formatTime2(new Date())
            //         wx.setStorageSync("time1Key",start_time); //缓存上车时间到本地
            //         that.setData({
            //             time1: wx.getStorageSync('time1Key'),
            //             isShow: true
            //         })
            //     } else {
            //         wx.showModal({
            //             title: '提示',
            //             content: '请扫描“下车二维码”',
            //             showCancel: false,
            //             confirmText: '返回',
            //             success: function (res) {
            //                 wx.reLaunch({
            //                     url: '/pages/index/index'
            //                 });
            //             }
            //         });
            //     }
            // } else {
            //     if (wx.getStorageSync('time1Key') != '') {
            //         wx.setStorageSync("station2Key", str); //缓存下车地点到本地
            //         that.setData({
            //             last_station: wx.getStorageSync('station2Key'),
            //         })
            
            //         var last_time = util.formatTime2(new Date())
            //         wx.setStorageSync("time2Key",last_time);
            //         that.setData({
            //             time2: last_time,
            //             isShow: true
            //         })
            //         DB.collection("user").add({
            //             data: {
            //                 date:that.data.date,  //日期
            //                 start_Time: wx.getStorageSync('time1Key'),  //上车时间
            //                 start_station: wx.getStorageSync('station1Key'),  //上车地点
            //                 last_Time: that.data.time2,  //下车时间
            //                 last_station: wx.getStorageSync('station2Key'),  //下车地点
            //                 bus: that.data.bus
            //             }
            //         })
            
            //         DB.collection("users").add({
            //             data:{
            //               openid:wx.getStorageSync("openidKey"),
            //               name:wx.getStorageSync("nameKey"),
            //               phone:wx.getStorageSync("phoneKey"),
            //               start_time:wx.getStorageSync("dateKey").toString() + " " + wx.getStorageSync("time1Key").toString(),
            //               last_time:wx.getStorageSync("dateKey").toString() + " " + wx.getStorageSync("time2Key").toString(),
            //               station:wx.getStorageSync("station1Key").toString() + " — " + wx.getStorageSync("station2Key").toString(),
            //               bus_info:wx.getStorageSync("busKey"),
            //               driver:wx.getStorageSync("driverKey"),
            //             }
            //         })
            
            //         wx.removeStorageSync("dateKey")  //删除缓存
            //         wx.removeStorageSync("time1Key")  
            //         wx.removeStorageSync("station1Key")  
            //         wx.removeStorageSync("station2Key")  
            //         wx.removeStorageSync("busKey")  
            //         wx.removeStorageSync("driverKey")  
            //     } else {
            //         wx.showModal({
            //             title: '提示',
            //             content: '请扫描“上车二维码”',
            //             showCancel: false,
            //             confirmText: '返回',
            //             success: function (res) {
            //                 wx.reLaunch({
            //                     url: '/pages/index/index'
            //                 });
            //             }
            //         });
            //     }
            // }
            /* -----------------------------------↑-↑- 接收二维码参数 -↑-↑----------------------------------- */

          },
          fail: function (res) {
            console.log(res);
          }
        })
      },
      fail:function(res){
        console.log("获取信息失败！");
        wx.showModal({
          title: '提示',
          content: '定位失败，请检查是否打开手机定位功能',
          showCancel: false,
          confirmText: '返回',
          success: function(res) {
            wx.reLaunch({
              url:'/pages/index/index'
            });
          }
      });
      }
    })
  },

  handleReturntap:function(){
    wx.reLaunch({
      url:'/pages/index/index'
    });
  }
})